package com.nothing.service.run;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.arcsoft.face.FaceInfo;
import com.nothing.service.FaceEngineService;
import com.nothing.util.ExcutorUtil;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ImageUtil;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;

/**
 * 
 * @author shíQíang㊚
 *
 */
public class ScanFace implements Runnable{
		
	private LinkedBlockingQueue<BufferedImage> faceQueue;//阻塞队列
	
	private FaceEngineService faceEngineService;
	private AtomicInteger count; 
	private ImageView faceOne;
    private ImageView faceTwo;
    private ImageView faceThree;
    private ImageView faceFour;
    private ImageView faceFive;
	public ScanFace(LinkedBlockingQueue<BufferedImage> queue,AtomicInteger count,
			FaceEngineService faceEngineService,
			ImageView faceOne,ImageView faceTwo,
			ImageView faceThree,ImageView faceFour,
			ImageView faceFive){
		this.faceQueue = queue;
		this.count = count;
		this.faceEngineService = faceEngineService;
		this.faceOne = faceOne;
		this.faceTwo = faceTwo;
		this.faceThree = faceThree;
		this.faceFour = faceFour;
		this.faceFive = faceFive;
	}
	
	@Override
	public void run(){
		try {
			while(ExcutorUtil.isScanRunning){
				BufferedImage data = faceQueue.poll(2, TimeUnit.SECONDS);//有数据时直接从队列的队首取走，无数据时阻塞，在2s内有数据，取走，超过2s还没数据，返回失败
                if (null != data){
                    // System.out.println("人脸扫描拿到数据");
                     List<FaceInfo> faces = faceEngineService.detectFaces(data);
                     if(CollectionUtil.isEmpty(faces)){
                    	 continue;
                     }
                     for(FaceInfo info : faces){ 
                		 int left = info.getRect ().getLeft ();
                         int top = info.getRect ().getTop ();
                         int width = info.getRect ().getRight () - left;
                         int height = info.getRect ().getBottom () - top;
                         WritableImage face = SwingFXUtils.toFXImage(ImageUtil.cut(data, new Rectangle(left,top,width,height)),null);
                         int imgCount = count.incrementAndGet();
                         if(imgCount % 5 == 0){
                        	 faceOne.setImage(face);
                         }else if(imgCount % 5 == 1){
                        	 faceTwo.setImage(face);
                         }else if(imgCount % 5 == 2){
                        	 faceThree.setImage(face);
                         }else if(imgCount % 5  == 3){
                        	 faceFour.setImage(face);
                         }else if(imgCount % 5  == 4){
                        	 faceFive.setImage(face);
                         } 
                     }  
                	 data.flush(); 
                }else{
                	//System.out.println("人脸扫描无数据...");
                }
			}
		} catch (Exception e){ 
			e.printStackTrace();
		} 
	}
	
}